import java.util.*;

public class HashMapTest {
    public static void main(String[] args) {

        /**
         * 将学生对象和学生的归属地通过键与值存储到map集合中
         */
        HashMap<Student,String> hashMap = new HashMap<Student,String>();
        hashMap.put(new Student("aaa",22),"beijing");
        hashMap.put(new Student("bbb",23),"dongguan");
        hashMap.put(new Student("ccc",24),"shenzhen");
        hashMap.put(new Student("ddd",25),"guangdong");

        Set<Student> keySet = hashMap.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()){
            Student key = (Student)it.next();
            String value = hashMap.get(key);
            System.out.println(key.getName()+":"+key.getAge()+":"+value);
        }

        System.out.println("--------------------------------------------");

        show_1();

    }

    private static void show_1() {
        TreeMap<Student,String> treeMap = new TreeMap<Student,String>(new ComparatorByName());
        treeMap.put(new Student("aaa",22),"beijing");
        treeMap.put(new Student("bbb",23),"dongguan");
        treeMap.put(new Student("ccc",24),"shenzhen");
        treeMap.put(new Student("ddd",25),"guangdong");

        //Set<Student> keySet = treeMap.keySet();
        //Iterator it = keySet.iterator();
        Iterator<Map.Entry<Student,String>> it = treeMap.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Student,String> me = it.next();
            Student key = me.getKey();
            String value = me.getValue();
            System.out.println(key.getName()+":"+key.getAge()+":"+value);
        }
    }
}
